package com.isoft.c2team3service2.controller;

import com.isoft.c2team3service2.bean.ResponseData;
import com.isoft.c2team3service2.dto.HotPostDTO;
import com.isoft.c2team3service2.dto.PostDTO;
import com.isoft.c2team3service2.entity.HotPost;
import com.isoft.c2team3service2.service.HotPostService;
import lombok.RequiredArgsConstructor;
import org.checkerframework.checker.interning.qual.CompareToMethod;
import org.springframework.data.repository.query.Param;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/api/hot-posts")
@RequiredArgsConstructor
public class HotPostController {

    private final HotPostService hotPostService;

    // 原有：查询热门帖子列表
    @GetMapping
    public ResponseEntity<List<PostDTO>> getHotPosts() {
        return ResponseEntity.ok(hotPostService.getHotPostList());
    }

    // 新增1：添加热门帖子（POST 请求，传 HotPost 实体）
    @PostMapping("/hotPostsAdd")
    public ResponseData<Boolean> addHotPost(@RequestParam Integer postId) {
        try {
            boolean success = hotPostService.addHotPost(postId);
            return new ResponseData<>(success ? 200 : 500, success ? "添加成功" : "添加失败", success);
        } catch (IllegalArgumentException e) {
            return new ResponseData<>(400, e.getMessage(), false);
        }
    }

    // 新增2：删除热门帖子（DELETE 请求，传 hotPostId）
    @PutMapping("/hotPostsDel")
    public ResponseData<Boolean> deleteHotPost(@RequestParam Integer postId) {
        try {
            boolean success = hotPostService.deleteHotPost(postId);
            return new ResponseData<>(success ? 200 : 500, success ? "删除成功" : "删除失败", success);
        } catch (IllegalArgumentException e) {
            return new ResponseData<>(400, e.getMessage(), false);
        }
    }

    // 新增3：修改热门帖子（PUT 请求，传 HotPost 实体）
    @PutMapping("/posts")
    public ResponseData<Boolean> updateHotPost(@RequestBody HotPost hotPost) {
        try {
            if (hotPost.getId() == null) {
                return new ResponseData<>(400, "热门帖子ID不能为空", false);
            }
            boolean success = hotPostService.updateHotPost(hotPost);
            return new ResponseData<>(success ? 200 : 500, success ? "修改成功" : "修改失败", success);
        } catch (IllegalArgumentException e) {
            return new ResponseData<>(400, e.getMessage(), false);
        }
    }

    // 新增4：查询单个热门帖子（GET 请求，传 hotPostId）
    @GetMapping("/{id}")
    public ResponseData<HotPostDTO> getHotPostById(@PathVariable("id") Long hotPostId) {
        HotPostDTO hotPostDTO = hotPostService.getHotPostById(hotPostId);
        if (hotPostDTO != null) {
            return new ResponseData<>(200, "查询成功", hotPostDTO);
        } else {
            return new ResponseData<>(404, "热门帖子不存在", null);
        }
    }

    // 新增5：校验 postId 是否已关联热门帖子（用于前端防重复）
    @GetMapping("/check/post/{postId}")
    public ResponseData<Boolean> checkPost(@PathVariable("postId") Integer postId) {
        boolean exists = hotPostService.existsByPostId(postId);
        return new ResponseData<>(200, exists ? "该帖子已为热门" : "该帖子可添加为热门", exists);
    }
}